From 8ba98560af6954e82d13a5a81c8276568783159e Mon Sep 17 00:00:00 2001 From: Christian Aistleitner Date: Tue, 3 Apr 2012 12:23:03 +0200 Subject: [PATCH] PHPUnit preparations for tests of maintenance Change-Id: Id1ac2f8b940e5417e0facaacb7d5ec30b48a9efb --- tests/TestsAutoLoader.php | 3 + tests/phpunit/StructureTest.php | 1 + tests/phpunit/maintenance/DumpTestCase.php | 359 +++++++++++++++++++++ tests/phpunit/suite.xml | 5 + 4 files changed, 368 insertions(+) create mode 100644 tests/phpunit/maintenance/DumpTestCase.php diff --git a/tests/TestsAutoLoader.php b/tests/TestsAutoLoader.php index 0c97daa0cc..83015581b4 100644 --- a/tests/TestsAutoLoader.php +++ b/tests/TestsAutoLoader.php @@ -25,6 +25,9 @@ $wgAutoloadClasses += array( //Selenium 'SeleniumTestConstants' => "$testFolder/selenium/SeleniumTestConstants.php", + //maintenance + 'DumpTestCase' => "$testFolder/phpunit/maintenance/DumpTestCase.php", + //Generic providers 'MediaWikiProvide' => "$testFolder/phpunit/includes/Providers.php", ); diff --git a/tests/phpunit/StructureTest.php b/tests/phpunit/StructureTest.php index 6953ab665f..17ea06c407 100644 --- a/tests/phpunit/StructureTest.php +++ b/tests/phpunit/StructureTest.php @@ -20,6 +20,7 @@ class StructureTest extends MediaWikiTestCase { 'MediaWikiLangTestCase', 'MediaWikiTestCase', 'PHPUnit_Framework_TestCase', + 'DumpTestCase', ) ); $testClassRegex = "^class .* extends ($testClassRegex)"; $finder = "find $rootPath -name '*.php' '!' -name '*Test.php'" . diff --git a/tests/phpunit/maintenance/DumpTestCase.php b/tests/phpunit/maintenance/DumpTestCase.php new file mode 100644 index 0000000000..a6fe5497dd --- /dev/null +++ b/tests/phpunit/maintenance/DumpTestCase.php @@ -0,0 +1,359 @@ +doEdit( $text, $summary ); + if ( $status->isGood() ) { + $value = $status->getValue(); + $revision = $value['revision']; + $revision_id = $revision->getId(); + $text_id = $revision->getTextId(); + if ( ( $revision_id > 0 ) && ( $text_id > 0 ) ) { + return array( $revision_id, $text_id ); + } + } + throw new MWException( "Could not determine revision id (" . $status->getXML() . ")" ); + } + + + /** + * gunzips the given file and stores the result in the original file name + * + * @param $fname string: filename to read the gzipped data from and stored + * the gunzipped data into + */ + protected function gunzip( $fname ) { + $gzipped_contents = file_get_contents( $fname ); + if ( $gzipped_contents === FALSE ) { + $this->fail( "Could not get contents of $fname" ); + } + // We resort to use gzinflate instead of gzdecode, as gzdecode + // need not be available + $contents = gzinflate( substr( $gzipped_contents, 10, -8 ) ); + $this->assertEquals( strlen( $contents ), + file_put_contents( $fname, $contents ), "# bytes written" ); + } + + /** + * obtains a new temporary file name + * + * The obtained filename is enlisted to be removed upon tearDown + * + * @returns string: absolute name of the temporary file + */ + protected function getNewTempFile() { + $fname = tempnam( wfTempDir(), 'MW_PHPUnit_' . get_class( $this ) . '_' ); + $this->tmpfiles[] = $fname; + return $fname; + } + + /** + * Default set up function. + * + * Clears $wgUser, and reports errors from addDBData to PHPUnit + */ + function setUp() { + global $wgUser; + + parent::setUp(); + + // Check if any Exception is stored for rethrowing from addDBData + // @see self::exceptionFromAddDBData + if ( $this->exceptionFromAddDBData !== null ) { + throw $this->exceptionFromAddDBData; + } + $this->tmpfiles = array(); + + $wgUser = new User(); + } + + /** + * Default tear down function + * + * Removes all files that have been allocated via self::getNewTempFile, even if + * they turn out to be (empty or non-empty) directories now. + */ + function tearDown() { + foreach ( $this->tmpfiles as $fname ) { + if ( is_file( $fname ) || ( is_link( $fname ) ) ) { + unlink( $fname ); + } elseif ( is_dir( $fname ) ) { + wfRecursiveRemoveDir( $fname ); + } + } + parent::tearDown(); + } + + + /** + * Step the current XML reader until node end of given name is found. + * + * @param $name string: name of the closing element to look for + * (e.g.: "mediawiki" when looking for ) + * + * @return bool: true iff the end node could be found. false otherwise. + */ + protected function skipToNodeEnd( $name ) { + while ( $this->xml->read() ) { + if ( $this->xml->nodeType == XMLReader::END_ELEMENT && + $this->xml->name == $name ) { + return true; + } + } + return false; + } + + /** + * Step the current XML reader to the first element start after the node + * end of a given name. + * + * @param $name string: name of the closing element to look for + * (e.g.: "mediawiki" when looking for ) + * + * @return bool: true iff new element after the closing of $name could be + * found. false otherwise. + */ + protected function skipPastNodeEnd( $name ) { + $this->assertTrue( $this->skipToNodeEnd( $name ), + "Skipping to end of $name" ); + while ( $this->xml->read() ) { + if ( $this->xml->nodeType == XMLReader::ELEMENT ) { + return true; + } + } + return false; + } + + /** + * Opens an XML file to analyze and optionally skips past siteinfo. + * + * @param $fname string: name of file to analyze + * @param $skip_siteinfo bool: (optional) If true, step the xml reader + * to the first element after + */ + protected function assertDumpStart( $fname, $skip_siteinfo = true ) { + $this->xml = new XMLReader(); + $this->assertTrue( $this->xml->open( $fname ), + "Opening temporary file $fname via XMLReader failed" ); + if ( $skip_siteinfo ) { + $this->assertTrue( $this->skipPastNodeEnd( "siteinfo" ), + "Skipping past end of siteinfo" ); + } + } + + /** + * Asserts that the xml reader is at the final closing tag of an xml file and + * closes the reader. + * + * @param $tag string: (optional) the name of the final tag + * (e.g.: "mediawiki" for ) + */ + protected function assertDumpEnd( $name = "mediawiki" ) { + $this->assertNodeEnd( $name, false ); + if ( $this->xml->read() ) { + $this->skipWhitespace(); + } + $this->assertEquals( $this->xml->nodeType, XMLReader::NONE, + "No proper entity left to parse" ); + $this->xml->close(); + } + + /** + * Steps the xml reader over white space + */ + protected function skipWhitespace() { + $cont = true; + while ( $cont && ( ( $this->xml->nodeType == XMLReader::WHITESPACE ) + || ( $this->xml->nodeType == XMLReader::SIGNIFICANT_WHITESPACE ) ) ) { + $cont = $this->xml->read(); + } + } + + /** + * Asserts that the xml reader is at an element of given name, and optionally + * skips past it. + * + * @param $name string: the name of the element to check for + * (e.g.: "mediawiki" for ) + * @param $skip bool: (optional) if true, skip past the found element + */ + protected function assertNodeStart( $name, $skip = true ) { + $this->assertEquals( $name, $this->xml->name, "Node name" ); + $this->assertEquals( XMLReader::ELEMENT, $this->xml->nodeType, "Node type" ); + if ( $skip ) { + $this->assertTrue( $this->xml->read(), "Skipping past start tag" ); + } + } + + /** + * Asserts that the xml reader is at an closing element of given name, and optionally + * skips past it. + * + * @param $name string: the name of the closing element to check for + * (e.g.: "mediawiki" for ) + * @param $skip bool: (optional) if true, skip past the found element + */ + protected function assertNodeEnd( $name, $skip = true ) { + $this->assertEquals( $name, $this->xml->name, "Node name" ); + $this->assertEquals( XMLReader::END_ELEMENT, $this->xml->nodeType, "Node type" ); + if ( $skip ) { + $this->assertTrue( $this->xml->read(), "Skipping past end tag" ); + } + } + + + /** + * Asserts that the xml reader is at an element of given tag that contains a given text, + * and skips over the element. + * + * @param $name string: the name of the element to check for + * (e.g.: "mediawiki" for ...) + * @param $text string|false: If string, check if it equals the elements text. + * If false, ignore the element's text + * @param $skip_ws bool: (optional) if true, skip past white spaces that trail the + * closing element. + */ + protected function assertTextNode( $name, $text, $skip_ws = true ) { + $this->assertNodeStart( $name ); + + if ( $text !== false ) { + $this->assertEquals( $text, $this->xml->value, "Text of node " . $name ); + } + $this->assertTrue( $this->xml->read(), "Skipping past processed text of " . $name ); + $this->assertNodeEnd( $name ); + + if ( $skip_ws ) { + $this->skipWhitespace(); + } + } + + /** + * Asserts that the xml reader is at the start of a page element and skips over the first + * tags, after checking them. + * + * Besides the opening page element, this function also checks for and skips over the + * title, ns, and id tags. Hence after this function, the xml reader is at the first + * revision of the current page. + * + * @param $id int: id of the page to assert + * @param $ns int: number of namespage to assert + * @param $name string: title of the current page + */ + protected function assertPageStart( $id, $ns, $name ) { + + $this->assertNodeStart( "page" ); + $this->skipWhitespace(); + + $this->assertTextNode( "title", $name ); + $this->assertTextNode( "ns", $ns ); + $this->assertTextNode( "id", $id ); + + } + + /** + * Asserts that the xml reader is at the page's closing element and skips to the next + * element. + */ + protected function assertPageEnd() { + $this->assertNodeEnd( "page" ); + $this->skipWhitespace(); + } + + /** + * Asserts that the xml reader is at a revision and checks its representation before + * skipping over it. + * + * @param $id int: id of the revision + * @param $summary string: summary of the revision + * @param $text_id int: id of the revision's text + * @param $text_bytes int: # of bytes in the revision's text + * @param $text_sha1 string: the base36 SHA-1 of the revision's text + * @param $text string|false: (optional) The revision's string, or false to check for a + * revision stub + */ + protected function assertRevision( $id, $summary, $text_id, $text_bytes, $text_sha1, $text = false ) { + + $this->assertNodeStart( "revision" ); + $this->skipWhitespace(); + + $this->assertTextNode( "id", $id ); + $this->assertTextNode( "timestamp", false ); + + $this->assertNodeStart( "contributor" ); + $this->skipWhitespace(); + $this->assertTextNode( "ip", false ); + $this->assertNodeEnd( "contributor" ); + $this->skipWhitespace(); + + $this->assertTextNode( "comment", $summary ); + + $this->assertNodeStart( "text", false ); + if ( $text_bytes !== false ) { + $this->assertEquals( $this->xml->getAttribute( "bytes" ), $text_bytes, + "Attribute 'bytes' of revision " . $id ); + } + + + if ( $text === false ) { + // Testing for a stub + $this->assertEquals( $this->xml->getAttribute( "id" ), $text_id, + "Text id of revision " . $id ); + $this->assertFalse( $this->xml->hasValue, "Revision has text" ); + $this->assertTrue( $this->xml->read(), "Skipping text start tag" ); + if ( ( $this->xml->nodeType == XMLReader::END_ELEMENT ) + && ( $this->xml->name == "text" ) ) { + + $this->xml->read(); + } + $this->skipWhitespace(); + } else { + // Testing for a real dump + $this->assertTrue( $this->xml->read(), "Skipping text start tag" ); + $this->assertEquals( $text, $this->xml->value, "Text of revision " . $id ); + $this->assertTrue( $this->xml->read(), "Skipping past text" ); + $this->assertNodeEnd( "text" ); + $this->skipWhitespace(); + } + + $this->assertTextNode( "sha1", $text_sha1 ); + + $this->assertNodeEnd( "revision" ); + $this->skipWhitespace(); + } + +} diff --git a/tests/phpunit/suite.xml b/tests/phpunit/suite.xml index 8c942a49a8..a03f392bc4 100644 --- a/tests/phpunit/suite.xml +++ b/tests/phpunit/suite.xml @@ -20,6 +20,11 @@ skins + + + maintenance + StructureTest.php -- 2.20.1